﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
public partial class order_updateorder : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    public void BindData()
    {
        string connet = "Data Source=orcl;User ID=booksale;Password=Qwertyuiop1;Unicode=True";
        string sqlstr = "select * from booksale.orderdetails order by 订单号";
        OracleConnection con = new OracleConnection(connet);
        OracleDataAdapter da = new OracleDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "订单号" };
        GridView1.DataBind();


    }
    protected void select_Click(object sender, EventArgs e)
    {
        string connet = "Data Source=orcl;User ID=booksale;Password=Qwertyuiop1;Unicode=True";
        string sqlstr = "select * from booksale.orderdetails where 订单号='" + num.Text + "' or 商品名称='" + name.Text + "' or 付款时间=to_date('" + date.Text + "','yyyy-mm-dd') or 状态='" + DropDownList1.SelectedValue.ToString() + "'";
        OracleConnection con = new OracleConnection(connet);
        OracleDataAdapter da = new OracleDataAdapter(sqlstr, con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "订单号" };
        GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        string num = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
        string author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim();
        string pub = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim();
        string price = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim();
        string date = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim();
        string update = "update booksale.orderdetails set 商品名称='" + name + "',数量='" + author + "',总价='" + pub + "',状态='" + price + "',用户='"+ date +"' where 订单号='" + num + "'";
        bool i = ConnectSQL(update);
        if (i)
        {
            Response.Write("<script language=javascript>alert('修改成功')</script");
            GridView1.EditIndex = -1;
            BindData();
        }
        else { Response.Write("<script language=javascript>alert('修改失败')</script"); }

    }
    public bool ConnectSQL(string strSqlCom)
    {
        string connet = "Data Source=orcl;User ID=booksale;Password=Qwertyuiop1;Unicode=True";
        OracleConnection con = new OracleConnection(connet);
        OracleCommand com = new OracleCommand(strSqlCom, con);
        try
        {
            if (con.State == System.Data.ConnectionState.Closed)
            { con.Open(); }
            com.ExecuteNonQuery();
            return true;


        }
        catch
        { return false; }
        finally
        { con.Close(); }
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.EditIndex = e.NewPageIndex;
        BindData();
    }
}